package com.tongguan.paypos.db;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.tongguan.paypos.entity.UserInfo;
import com.tongguan.paypos.utils.L;

import java.util.ArrayList;

/**
 * Created by Joylive on 2016/12/7 .
 */

public class UserDao {

    private static final String TAG = "UserDao";

    public static void addAccount(Context context, String account, String pwd){
        SQLiteDatabase db = null;
        DataBaseOpenHelper dbHelper = new DataBaseOpenHelper(context);
        try {
            db = dbHelper.getReadableDatabase();

            Cursor cursor = db.rawQuery(String.format("SELECT * FROM user WHERE account = %s", account), null);
            if(cursor.getCount() <= 0){
                db.execSQL("INSERT INTO user (account, password) VALUES(?,?)", new Object[]{account, pwd});
            }else{
                db.execSQL("UPDATE user SET password = ? WHERE account = ?", new Object[]{pwd, account});
            }
        } catch (Exception e) {
            L.e(TAG, e.toString());
        }finally{
            if(db != null){
                db.close();
            }
        }
    }
    public static void delAccount(Context context, String account){
        SQLiteDatabase db = null;
        DataBaseOpenHelper dbHelper = new DataBaseOpenHelper(context);
        try {
            db =  dbHelper.getReadableDatabase();
            db.execSQL(String.format("DELETE FROM user WHERE account = %s", account));
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(db != null){
                db.close();
            }
        }
    }
    public static String getPwd(Context context, String account){
        String pwd = "";
        SQLiteDatabase db = null;
        DataBaseOpenHelper dbHelper = new DataBaseOpenHelper(context);
        try {
            db = dbHelper.getReadableDatabase();
            Cursor cursor =  db.rawQuery(String.format("SELECT password FROM user WHERE account = %s LIMIT 1", account), null);
            if(cursor.moveToFirst()){
                pwd = cursor.getString(cursor.getColumnIndex("password"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(db != null){
                db.close();
            }
        }
        return pwd;
    }

    public static ArrayList<UserInfo> getAccountList(Context context){
        SQLiteDatabase db = null;
        DataBaseOpenHelper dbHelper = new DataBaseOpenHelper(context);
        ArrayList<UserInfo> dataList = new ArrayList<>();
        try {
            db = dbHelper.getReadableDatabase();

            Cursor cursor = db.rawQuery("SELECT * FROM user", null);
            UserInfo userInfo =null;
            while(cursor.moveToNext()){
                userInfo = new UserInfo();
                userInfo.setAccount(cursor.getString(cursor.getColumnIndex("account")));
                userInfo.setPassword(cursor.getString(cursor.getColumnIndex("password")));
                dataList.add(userInfo);
            }
        } catch (Exception e) {

        }finally{
            if(db != null){
                db.close();
            }
        }
        return dataList;
    }


}
